package com.property.manage.service.equipment;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.guardchina.framework.util.utils.DateUtil;
import com.guardchina.framework.util.utils.PhoneUtil;
import com.property.manage.dao.equipment.EquipmentMapper;
import com.property.manage.dao.parking.ParkingMapper;
import com.property.manage.model.equipment.EquipmentEntity;
import com.property.manage.model.equipment.EquipmentVo;
import com.property.manage.model.parking.ParkingEntity;
import com.property.manage.model.parking.ParkingVo;
import com.property.manage.model.village.Village;
import com.property.manage.service.village.VillageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
public class EquipmentService {
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private VillageService villageService;
    @Value("${timed.tasks}")
    private  Integer day;


    public EquipmentVo findParking(Integer id) {
        EquipmentVo parkingVo = new EquipmentVo();
        EquipmentEntity entity = equipmentMapper.selectById(id);
        if (entity == null) {
            return parkingVo;
        }
        BeanUtils.copyProperties(entity, parkingVo);
        Village village = villageService.findVillage(entity.getVillageId());
        if (village == null) {
            throw new BusinessException("小区信息错误", String.format("小区id[%s]", entity.getVillageId()));
        }
        parkingVo.setVillageName(village.getName());
        return parkingVo;
    }

    public CustomPage<EquipmentVo> page(Long pageNo, Long pageSize, Integer villageId,
                                      String name) {
        Page<EquipmentVo> page = new Page<>(pageNo, pageSize);
        IPage<EquipmentVo> village = equipmentMapper.getPage(page,villageId,name);
        return new CustomPage<>(village);
    }

    /**
     * 设备维修到期列表
     * @param pageNo
     * @param pageSize
     * @param villageId
     * @return
     */
    public CustomPage<EquipmentVo> getRepairPage(Long pageNo, Long pageSize, Integer villageId) {
        Page<EquipmentVo> page = new Page<>(pageNo, pageSize);
        String after = getNextTime();
        IPage<EquipmentVo> village = equipmentMapper.getRepairPage(page,villageId, after);
        return new CustomPage<>(village);
    }

    private String getNextTime() {
        String aShort = DateUtil.getStringDateShort(new Date());
        log.info("获取到的值数{}",day);
        return DateUtil.getDateAfter(aShort, day);
    }

    public void add(String name,String number,String dateManufacture,Integer villageId,String dateUse,
                    String nextTime,String address
    ) {
        if (querNumber(number, villageId)) {
            throw new BusinessException("编号已经存在，请勿重复添加", String.format("编号[%s]", number));
        }
        EquipmentEntity entity = new EquipmentEntity();
       entity.setAddress(address);
       entity.setDateManufacture(dateManufacture);
       entity.setDateUse(dateUse);
       entity.setName(name);
       entity.setNextTime(nextTime);
       entity.setNumber(number);
       entity.setVillageId(villageId);
       equipmentMapper.insert(entity);
    }

    public void update(Integer id,String name,String number,String dateManufacture,String dateUse,
                       String nextTime,String address) {
        EquipmentVo parking = this.findParking(id);
        if (parking == null) {
            throw new BusinessException("id错误", String.format("车位id[%s]", id));
        }
        if (!number.equals(parking.getNumber())) {
            if (querNumber(number, parking.getVillageId())) {
                throw new BusinessException("编号已经存在，请勿重复添加", String.format("编号[%s]", number));
            }
        }
        UpdateWrapper<EquipmentEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("name", name);
        wrapper.set("number", number);
        wrapper.set("date_manufacture", dateManufacture);
        wrapper.set("date_use", dateUse);
        wrapper.set("next_time", nextTime);
        wrapper.set("address", address);
        wrapper.eq("id", id);
        equipmentMapper.update(null, wrapper);
    }

    /**
     * 修改下次维修时间
     * @param id
     * @param nextTime
     */
    public void updateNextTime(Integer id,String nextTime){
        UpdateWrapper<EquipmentEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("next_time", nextTime);
        wrapper.eq("id", id);
        equipmentMapper.update(null, wrapper);
    }

    public void delete(Integer id) {
        equipmentMapper.deleteById(id);
    }

    public boolean querNumber(String number, Integer villageId) {
        QueryWrapper<EquipmentEntity> qw = new QueryWrapper<>();
        qw.eq("number", number);
        qw.eq("village_id", villageId);
        return equipmentMapper.selectOne(qw) != null;
    }

    public int querEqId(String name,String number, Integer villageId) {
        QueryWrapper<EquipmentEntity> qw = new QueryWrapper<>();
        qw.eq("number", number);
        qw.eq("village_id", villageId);
        qw.eq("name", name);
        EquipmentEntity entity = equipmentMapper.selectOne(qw);
        if (entity==null){
            throw new BusinessException("设备基本信息错误",String.format("设备基本信息:名称[%s]，小区id[%s],编号[%s]",name,villageId,number));
        }
        return entity.getId();
    }

   public Integer getRepairCount(Integer villageId){
        return equipmentMapper.getRepairCount(villageId,getNextTime());
    }
}
